package com.jdd.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.dao.entity.TDeviceStatus;
import com.jdd.dao.mapper.TDeviceStatusMapper;
import com.jdd.dao.service.TDeviceStatusService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 设备指令状态表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-25
 */
@Service
public class TDeviceStatusServiceImpl extends ServiceImpl<TDeviceStatusMapper, TDeviceStatus> implements TDeviceStatusService {

    /**
     * @param status 1-下线2-在线
     * @param device 设备对象
     * @return void
     * @Description 更新设备状态
     * @Author zsj
     * @Date 2022-03-26 9:56
     **/
    @Override
    public void updateDeviceStatus(Integer status, TDevice device) {
        QueryWrapper<TDeviceStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_sn", device.getDeviceSn());
        TDeviceStatus deviceStatus = this.baseMapper.selectOne(queryWrapper);
        if (null != deviceStatus) {
            deviceStatus.setStatus(status);
            this.baseMapper.updateById(deviceStatus);
            return;
        }
        deviceStatus = new TDeviceStatus();
        deviceStatus.setDeviceId(device.getId());
        deviceStatus.setDeviceSn(device.getDeviceSn());
        deviceStatus.setStatus(status);
        this.baseMapper.insert(deviceStatus);
    }
}
